package com.aviccxzx.smarthome.UserModule;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aviccxzx.smarthome.CommonModule.GosBaseActivity;
import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.entity.CamShareRecordBean;
import com.aviccxzx.smarthome.service.RetrofitHelper;
import com.aviccxzx.smarthome.service.RetrofitService;
import com.aviccxzx.smarthome.utils.DialogUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by joey on 2016/10/25.
 */
public class ShareCamrecordActivity extends GosBaseActivity implements View.OnClickListener {
    CamShareAdapter shareAdapter;
    ListView share_list;
    String token = "";
    String yst = "";
    List<CamShareRecordBean.DataBean> sharelist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.cam_sharelist_lay_v19);
        } else {
        }
        ((TextView) findViewById(R.id.title_tv)).setText("摄像机分享记录");
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        yst = getIntent().getStringExtra("yst");
        share_list = (ListView) findViewById(R.id.share_list);
        sharelist = new ArrayList<>();
        shareAdapter = new CamShareAdapter();
        share_list.setAdapter(shareAdapter);
        getShareRecord();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_title_left_view:
                finish();
                break;
        }
    }

    public void getShareRecord() {
        progressDialog.show();
        RetrofitService service = RetrofitHelper.getInstance(this).getServer();
        Call<CamShareRecordBean> call = service.getCamshare(yst);
        call.enqueue(new Callback<CamShareRecordBean>() {
            @Override
            public void onResponse(Call<CamShareRecordBean> call, Response<CamShareRecordBean> response) {
                progressDialog.dismiss();
                if (response.body() != null && response.body().getData() != null) {
                    sharelist.addAll(response.body().getData());
                    shareAdapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFailure(Call<CamShareRecordBean> call, Throwable t) {
                progressDialog.dismiss();
            }
        });
    }


    public class CamShareAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return sharelist.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int i, View convertView, ViewGroup viewGroup) {
            View view = null;

            if (convertView != null) {
                view = convertView;
            } else {
                view = LayoutInflater.from(ShareCamrecordActivity.this).inflate(R.layout.cam_sharelist_item_lay, viewGroup, false);
            }

            ViewHolder holder = (ViewHolder) view.getTag();
            if (holder == null) {
                holder = new ViewHolder();
                view.setTag(holder);
            }
            holder.share_delete_img = (ImageView) view.findViewById(R.id.share_delete_img);
            holder.share_name_tv = (TextView) view.findViewById(R.id.share_name_tv);
            holder.share_name_tv.setText(sharelist.get(i).getPhone());
            holder.share_delete_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new DialogUtil(ShareCamrecordActivity.this, true, true, "确定要取消分享吗?", "取消", "确定", new DialogUtil.SureInterfance() {
                        @Override
                        public void sureTodo() {
                            deleteShareCam(sharelist.get(i).getUid());
                        }
                        @Override
                        public void cancle() {

                        }
                    }).showCustomDialog();
                }
            });
            return view;
        }

        public void deleteShareCam(String uid) {
            progressDialog.show();
            RetrofitService service = RetrofitHelper.getInstance(ShareCamrecordActivity.this).getServer();
            Call<ResponseBody> call = service.deleteShareCam(yst, uid);
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    getShareRecord();
                    progressDialog.dismiss();
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    progressDialog.dismiss();
                }
            });
        }

        public class ViewHolder {
            TextView share_name_tv;
            ImageView share_delete_img;
        }
    }
}
